Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
题目大意:展平二叉树
题目难度:Medium
/**
* Created by gzdaijie on 16/6/9
*/
public class Solution {
public void flatten(TreeNode root) {
if (root == null) return;
flatten(root.left);
flatten(root.right);
if (root.left != null) {
TreeNode tmp = root.right;
TreeNode tail = root.left;
while (tail.right != null) tail = tail.right;
root.right = root.left;
tail.right = tmp;
root.left = null;
}
}
}